package halberd.filter;

import halberd.DataType;
import halberd.ImgFilter;
import halberd.data.ImgData;
import halberd.data.ImgDataFactory;
import halberd.utils.RGBUtils;
/**
 * rgb 转灰度
 * @author jiang
 *
 */
public class GrayFilter implements ImgFilter {

	public ImgData process(ImgData input) {
		if(!canProcess(input)) {
			return null;
		}
		ImgData result=ImgDataFactory.createImgData(input.getWidth(), input.getHeight());
		rgb2gray(input.getDatas(),result.getDatas(),input.getWidth(),input.getHeight());
		result.setDataType(DataType.GRAY);
		return result;
	}
	private boolean canProcess(ImgData input) {
		if(input==null) {
			return false;
		}
		DataType dt=input.getDataType();
		if(dt==DataType.RGB) {
			return true;
		}
		if(dt==DataType.RGBA) {
			return true;
		}
		return false;
	}
	private void rgb2gray(int[][][] src,int[][][] dst,int w,int h) {
		for(int i=0;i<w;++i) {
			for(int j=0;j<h;++j) {
				dst[i][j][4] = RGBUtils.rgb2gray(src[i][j][0], src[i][j][1], src[i][j][2]);
			}
		}
	}
}
